package com.zjj.algorithm.learning.atguigu.sparsearray;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 二维数组向稀疏数组的转化
 *
 * @author zjj_admin
 * @date 2022/10/30 18:44
 */
public class SparseArray {

    public static void main(String[] args) {

        //生成一个原始的 11 * 11 的二维数组
        //黑子为 1，蓝子为 1，无子为 0
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[5][6] = 2;

//        parseSerialize(chessArr1);

        //打印原始数组
        System.out.println("原始数组为");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.print(data + " ");
            }
            System.out.println();
        }
        System.out.println("数组的长度为：" + chessArr1.length);


        //将稀疏数组转化为二维数组的思想
        //1、遍历二维数组，获取未0 数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("sum=" + sum);
        //2、创建稀疏数组
        int[][] sparseArray = new int[sum + 1][3];
        //给稀疏数组赋值
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;

        //将二维数组的非0值存放到稀疏数组中
        // count 表示当前已经存在多少个非 0 数
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessArr1[i][j];
                }
            }
        }

        //稀疏数组创建成功
        //输出稀疏数组
        System.out.println("得到的稀疏数组");
        for (int i = 0; i < sparseArray.length; i++) {
//            System.out.println(" " + sparseArray[i][0] + " " + sparseArray[i][1] + " " + sparseArray[i][2]);
            System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);
        }


        //将稀疏数组回复称原始的数组
        //1、先读取第一行，判断有多少行，多少列
        int[][] chessArr2 = new int[sparseArray[0][0]][sparseArray[0][1]];

        //将稀疏数组中的数据赋给原始数组
        for (int i = 1; i < sparseArray.length; i++) {
            chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }

        System.out.println("转化后原始数组为");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.print(data + " ");
            }
            System.out.println();
        }


    }


    private static String resourcePath =
            "D:\\workspace idea\\algorithm-learning\\src\\main\\resources\\stguigu\\sparsearray\\file.txt";

    /**
     * 给定一个数组，将数组中的数据写入到一个文件中
     *
     * @param array
     */
    private static void parseSerialize(int[][] array) {
        ObjectOutputStream oos = null;
        try {
            FileOutputStream fos = new FileOutputStream(resourcePath);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(array);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
